#include "jdy25m.h"

uint8_t jdy25m_init(struct jdy25mStu *jdy){
    uint8_t i;
    HAL_StatusTypeDef hStatus;
    uint8_t jdyBuffer[30] = {0, 0, 0, [16] = 0};

    for(i = 0; i < 30; i ++){
        jdyBuffer[i] = 0;
    }

    HAL_UART_Transmit(jdy->huart, "AT\r\n", 4, 0xFFFF);
    hStatus = HAL_UART_Receive(jdy->huart, jdyBuffer, 3, 0x1FF);
    if(hStatus == HAL_TIMEOUT){
        printf_DMA(PRINT_ERROR"JDY-25M not online!\n");
        printf_DMA(PRINT_DEBUG"Buffer[0] = 0x%x, Buffer[1] = 0x%x, Buffer[2] = 0x%x\n", jdyBuffer[0], jdyBuffer[1], jdyBuffer[2]);
        
        return 1;
    }else if(hStatus != HAL_OK){
        printf_DMA(PRINT_ERROR"JDY25-M uart Error: %d\n", hStatus);
        return 2;
    }
    if(jdyBuffer[0] != '+' || jdyBuffer[1] != 'O' || jdyBuffer[2] != 'K'){
        if(jdyBuffer[0] != 'O' || jdyBuffer[1] != 'K'){
        printf_DMA(PRINT_ERROR"Unknown ACK from JDY-25M: %s\n", jdyBuffer);
        return 3;
        }
    }

    HAL_UART_Transmit(jdy->huart, "AT+ROLE5\r\n", 10, 0xFFFF);
    HAL_Delay(20);
    HAL_UART_Transmit(jdy->huart, "AT+NETID"JDY25M_SELF_MESH_NET_ID"\r\n", 14, 0xFFFF);
    HAL_Delay(20);
    HAL_UART_Transmit(jdy->huart, "AT+MADDR"JDY25M_SELF_MESH_ADDR"\r\n", 14, 0xFFFF);
    HAL_Delay(20);
    HAL_UART_Transmit(jdy->huart, "AT+MCLSS"JDY25M_SELF_MESH_DEVTYPE"\r\n", 11, 0xFFFF);
    HAL_Delay(20);
    HAL_UART_Transmit(jdy->huart, "AT+NAME"JDY25M_SELF_MESH_NAME"\r\n", 9 + strlen(KORO_MESH_STAFF_NAME), 0xFFFF);
    HAL_Delay(20);

    HAL_UART_Transmit(jdy->huart, "AT+RESET\r\n", 10, 0xFFFF);
    // HAL_UART_Receive(jdy->huart, jdyBuffer, 25, 0x1FF);

    printf_DMA(PRINT_DEBUG"JDY-25M init msg send over, reset...\n");
    // for(i = 0; i < 30; i ++){
    //     jdyBuffer[i] = 0;
    // }
    // HAL_Delay(20);
    // while(1){
    //     HAL_UART_Transmit(jdy->huart, "AT\r\n", 4, 0xFFFF);
    //     hStatus = HAL_UART_Receive(jdy->huart, jdyBuffer, 5, 0x1FF);
    //     if(hStatus == HAL_TIMEOUT){
    //         // printf_DMA(PRINT_ERROR"JDY-25M not online!\n");
    //         // printf_DMA(PRINT_DEBUG"Buffer[0] = 0x%x, Buffer[1] = 0x%x, Buffer[2] = 0x%x\n", jdyBuffer[0], jdyBuffer[1], jdyBuffer[2]);
    //     }else if(hStatus != HAL_OK){
    //         printf_DMA(PRINT_ERROR"JDY-25M uart Error: %d\n", hStatus);
    //     }
    //     if(jdyBuffer[0] != 'O' || jdyBuffer[1] != 'K'){
    //         printf_DMA(PRINT_ERROR"Unknown ACK from JDY-25M: %s\n", jdyBuffer);
    //     }else {
    //         break;
    //     }
    // }
    // HAL_Delay(20);
    // HAL_UART_Receive(jdy->huart, jdyBuffer, 25, 0x1FF);
    
    // HAL_UART_Transmit(jdy->huart, "AT+NAME\r\n", 9, 0xFFFF);
    // HAL_UART_Receive(jdy->huart, jdyBuffer, 25, 0x1FF);
    // if((jdyBuffer[6] != 'S') || (jdyBuffer[7] != 't') || (jdyBuffer[8] != 'a')){
    //     printf_DMA(PRINT_ERROR"Init JDY-25M Failed\n");
    //     printf_DMA(PRINT_DEBUG"Buffer[6] = 0x%x, Buffer[7] = 0x%x, Buffer[8] = 0x%x\n", jdyBuffer[0], jdyBuffer[1], jdyBuffer[2]);
    //     printf_DMA(PRINT_ERROR"Recived: %s\n", jdyBuffer);
    //     return 4;
    // }

    return 0;
}

void jdy25m_mesh_send(struct jdy25mStu *jdy, uint16_t recvMADDR, uint8_t *data, uint8_t size, JDY25M_MESH_CMD_E cmd){
    uint8_t i;
    uint8_t meshBuffer[25] = {
        [0] = 'A',
        [1] = 'T',
        [2] = '+',
        [3] = 'M',
        [4] = 'E',
        [5] = 'S',
        [6] = 'H',
    };

    meshBuffer[7] = cmd;
    meshBuffer[8] = (uint8_t)(recvMADDR >> 8);
    meshBuffer[9] = (uint8_t)(recvMADDR&0xFF);

    for(i = 0; i < size; i ++){
        meshBuffer[10 + i] = data[i];
    }

    meshBuffer[10 + i] = '\r';
    meshBuffer[11 + i] = '\n';

    // 如果是从 mesh 命令调用这个函数可以用 dma，读完卡调用则控制器收不到消息，之前好像没这个问题，无语，折腾半天
    // HAL_UART_Transmit_DMA(jdy->huart, meshBuffer, 12+i);
    HAL_UART_Transmit(jdy->huart, meshBuffer, 12+i, 0xFFFF);
}

